# 发起商家转账API
module Wechatpay
  module Transfer
    # 1001-现金营销
    def transfer_bills(transfer_amount:, openid:, out_bill_no:, info_type:, info_content:, transfer_scene_id: '1000', transfer_remark: '红包', notify_url: '')
      url = "#{V3_PATH}/fund-app/mch-transfer/transfer-bills"
      transfer_scene_report_infos = [
        {
          info_type: "活动名称",
          info_content: info_type
        },
        {
          info_type: "奖励说明",
          info_content: info_content
        }
      ]

      refresh_nonce

      pp transfer_scene_report_infos
      params = {
        openid:,
        transfer_amount:,
        transfer_remark:,
        out_bill_no:,
        transfer_scene_report_infos:,

        transfer_scene_id:,
        appid: @client.id,
      }
      if notify_url.present?
        params[:notify_url] = notify_url
      end
      pp params.to_json
      post url, params.to_json
    end

    # TODO: remove
    # deprecated
    def cash_to_wallet(name:, batch_no:, remark:, lists:)
      url = "#{V3_PATH}/transfer/batches"
      amount = lists.sum {_1[:transfer_amount]}
      refresh_nonce
      params = {
        out_batch_no: batch_no,
        batch_name: name,
        batch_remark: remark,
        total_amount: amount,
        total_num: lists.size,
        appid: @client.id,
        stock_creator_mchid: @client.mch_id,
        transfer_detail_list: lists
      }
      post url, params.to_json
    end

    # 由于转账不能直接返回结果
    # 需要手动查询
    def verify_wallet_results(batch_id: nil, out_batch_no: nil)
      url = "#{V3_PATH}/transfer/batches"
      url = if out_batch_no.nil?
              [url, 'batch-id', batch_id].join('/')
            else
              [url, 'out-batch-no', out_batch_no].join('/')
            end
      params = {
        need_query_detail: true,
        detail_status: 'FAIL'
      }.to_query
      refresh_nonce
      query [url, params].join('?')
    end

    # 查看单个详情
    def verify_trans_detail(out_batch_no:, out_detail_no:)
      url = "#{V3_PATH}/transfer/batches"
      url = [url, 'out-batch-no',
             out_batch_no,
             'details',
             'out-detail-no',
             out_detail_no
            ].join('/')
      refresh_nonce
      query url
    end

  end
end
